#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include "/usr/include/mosquitto.h"

int main(int argc, char const *argv[])
{
    // 初始化mqtt库
    mosquitto_lib_init();

    // 创建一个mqtt客户端对象
    struct mosquitto *mosq = mosquitto_new(NULL, true, NULL);
    if(mosq == NULL)
    {
        printf("创建mosquitto客户端对象失败\n");
        return -1;
    }

    // 连接mqtt服务器
    int ret = mosquitto_connect(mosq, "120.26.28.32", 1883, 60);
    if(ret != MOSQ_ERR_SUCCESS)
    {
        printf("连接mqtt服务器失败\n");
        return -1;
    }
    else
    {
        printf("连接mqtt服务器成功\n");
    }
    
    // 开启消息循环线程
    mosquitto_loop_start(mosq);
    while(1)
    {
        char msg[100] = "泥嚎，我是一个发布端";
        mosquitto_publish(mosq, NULL, "text", strlen(msg), msg, 0, false);
        sleep(1);
    }

    return 0;
}
